package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysMoment;
import com.ruoyi.project.system.domain.SysMomentCollection;
import com.ruoyi.project.system.domain.SysMomentFile;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMomentCollectionService;
import com.ruoyi.project.system.service.ISysMomentFileService;
import com.ruoyi.project.system.service.ISysMomentService;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收藏平台瞬间Controller
 *
 * @author jjp
 * @date 2020-07-21
 */
@RestController
@Api(description = "用户收藏平台瞬间")
@RequestMapping("/mobileUser/collection")
public class UserMomentCollectionController extends BaseController {
    @Autowired
    private ISysMomentCollectionService sysMomentCollectionService;
    @Autowired
    private ISysMomentService momentService;
    @Autowired
    private ISysMomentFileService sysMomentFileService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户收藏平台瞬间列表
     */
    @ApiOperation("查询用户收藏平台瞬间列表")
    @GetMapping("/list")
    public TableDataInfo list(SysMomentCollection sysMomentCollection) {
        startPage();
        sysMomentCollection.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysMomentCollection> list = sysMomentCollectionService.selectSysMomentCollectionList(sysMomentCollection);

        for (int i = 0; i < list.size(); i++) {
            Long momentId = list.get(i).getMomentId();
            SysMoment sysMoment = momentService.selectSysMomentById(momentId);
            List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
            SysUser sysUser = sysUserService.selectUserGetUserName(sysMoment.getCreateBy());
            sysMoment.setSysUser(sysUser);
            sysMoment.setSysMomentFile(sysMomentFiles);
            list.get(i).setSysMoment(sysMoment);
        }
        return getDataTable(list);
    }



    /**
     * 新增用户收藏平台瞬间
     */
    @ApiOperation("新增用户收藏平台瞬间")
    @Log(title = "用户收藏平台瞬间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMomentCollection sysMomentCollection) {
        sysMomentCollection.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        return toAjax(sysMomentCollectionService.insertSysMomentCollection(sysMomentCollection));
    }


    /**
     * 删除用户收藏平台瞬间
     */
    @ApiOperation("删除用户收藏平台瞬间")
    @Log(title = "用户收藏平台瞬间", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody SysMomentCollection sysMomentCollection) {
        sysMomentCollection.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysMomentCollection> sysMomentCollectionList = sysMomentCollectionService.selectSysMomentCollectionList(sysMomentCollection);
        SysMomentCollection momentCollection = sysMomentCollectionList.get(0);
        return toAjax(sysMomentCollectionService.deleteSysMomentCollectionById(momentCollection.getCollectionId()));
    }
}
